package jjn.hot;

import commons.BinaryTreeHelper;
import commons.TreeNode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * @author Jjn
 * @since 2023/8/31 23:53
 */
public class LeetCode513 {
    public int findBottomLeftValue(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.offer(root);
        int val = 0;
        while (!deque.isEmpty()) {
            int size = deque.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = deque.pollFirst();
                val = node.val;
                if (node.right != null) {
                    deque.offer(node.right);
                }
                if (node.left != null) {
                    deque.offer(node.left);
                }
            }
        }
        return val;
    }
    
    public static void main(String[] args) {
        List<Integer> input = new ArrayList<>();
        input.addAll(List.of(1, 2, 3, 4));
        input.add(null);
        input.addAll(List.of(5, 6));
        input.add(null);
        input.add(null);
        input.add(7);
        TreeNode node = BinaryTreeHelper.buildTree(input);
        int bottomLeftValue = new LeetCode513().findBottomLeftValue(node);
        System.out.println(bottomLeftValue);
    }
}
